//
//  ViewController.m
//  IMChezhil
//
//  Created by zhangliucheng on 15/6/26.
//  Copyright (c) 2015年 CheZhil. All rights reserved.
//

#import "MainViewController.h"
#import "LoginViewController.h"
#import "IMCore.h"
#import "AppDelegate.h"

@interface MainViewController() <UIAlertViewDelegate, IMNotificationReceiverDelegate>
@property (weak, nonatomic) IBOutlet UILabel *number;

@property (weak, nonatomic) IBOutlet UILabel *test;

@end

@implementation MainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[IMCore shareInstance] registerNotificationReceiveDelegate:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self setUnReadNumber];
    self.test.text = @"\U00002652";
}

// 设置消息未读数量
- (void)setUnReadNumber {
    NSInteger count = [[IMCore shareInstance] getMessageUnReadCount];
    if (count == 0) {
        self.number.hidden = YES;
    } else {
        self.number.hidden = NO;
        self.number.text = [NSString stringWithFormat:@"%ld", count];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)askQuestion:(id)sender {
    MainViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Chat"];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)logout:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否退出账号" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    alertView.tag = 1;
    [alertView show];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        
        if (buttonIndex == 1) {
            [[IMCore shareInstance] logout];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
            delegate.window.rootViewController = navController;
        }
        
    } else if (alertView.tag == 2) {
        // 跳转到登陆界面
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        delegate.window.rootViewController = navController;
    }
}

#pragma mark - IMNotificationReceiverDelegate
- (void)OnOtherLoginBlock {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的账号再别的设备上登录，点击确认，将跳到登录界面" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
    alertView.tag = 2;
    [alertView show];
}

- (void)OnMessageNotificater:(IMUserMessage *)message {
    [self setUnReadNumber];
}
@end
